// pages/checkFlight/checkFlight.js
const app = getApp()
var utils = require('../../../utils/util.js')
import { getCity, getDefCity } from '../../../utils/cityJson'
import { getLangCityName } from '../../../utils/cityJson'
import { getPosterList } from '../../../api/advert/advert'
import { _t } from '../../../i18n/base'
Page({
  data: {
    showDialog: false,
    showCalendar: false,
    date: utils.formatDate(new Date()),
    flightno: '',
    selItem: 1,
    scrollTop: 0,
    dCityTxt: '',
    aCityTxt: '',
    chooseType: 1,
    borderType: 1,
    lang: '',
    langData: {},
    citys: {},
    getAdvertObj: {
      categoryType: '',
      language: '',
      posterName: '',
      posterType: 8,
      showType: 1,
      trafficType: 1,
    },
    bannerList: [],
  },
  // 打开日历
  onCalendarShow() {
    this.setData({
      showCalendar: true,
    })
  },
  // 输入航班号
  changeFlightno(e) {
    this.setData({
      flightno: e.detail.value,
    })
  },
  // 确认日期
  confirmDateHandle(e) {
    console.log(e)
    this.setData({
      showCalendar: false,
      date: e.detail.date,
    })
  },
  // 关闭日历
  onCalendarClose() {
    this.setData({
      showCalendar: false,
    })
  },
  // 确认日历
  // onCalendarConfirm(event) {
  //   this.setData({
  //     showCalendar: false,
  //     date: utils.formatDate(event.detail),
  //   })
  // },
  // 修改城市
  onChangeCity(event) {
    const { picker, value, index } = event.detail
    picker.setColumnValues(1, this.data.cityParse[value[0]])
  },
  // 打开城市选择
  chooseCity(event) {
    let selItem = event.target.dataset.item
    if (this.data.borderType == 2) {
      this.setData({
        // gobalCityShow: true,
        selItem: selItem,
      })
      let title = selItem == 1 ? this.data.langData['出发地'] : this.data.langData['目的地']
      wx.navigateTo({
        url: '/pages/gobalcity/gobalcity?selItem=' + selItem + '&title=' + title,
      })
    } else {
      this.setData({
        showDialog: true,
        selItem: event.target.dataset.item,
      })
    }
  },
  // 关闭城市选择
  onCancelCity() {
    this.setData({
      showDialog: false,
    })
  },
  // 确认城市
  onConfirmCity(event) {
    const { picker, value, index } = event.detail
    let d = {
      showDialog: false,
    }
    this.data.selItem == 1 ? (d.dCityTxt = value[1]) : (d.aCityTxt = value[1])
    this.setData(d)
  },
  // 切换境内境外
  borderChooseHandle(e) {
    this.setData({
      borderType: e.currentTarget.dataset.type,
      flightno: '',
      dCityTxt: '',
      aCityTxt: '',
    })
  },
  // 切换查询选项
  chooseTypeHandle(e) {
    this.setData({
      chooseType: e.currentTarget.dataset.type,
    })
  },
  // 查询按钮
  check() {
    let type = this.data.chooseType
    let obj = {}
    // debugger
    if (type == 1) {
      obj = {
        date: this.data.date,
        flightno: this.data.flightno,
        borderType: this.data.borderType,
      }
    } else {
      obj = {
        date: this.data.date,
        borderType: this.data.borderType,
        dCityTxt: getLangCityName(this.data.dCityTxt, this.data.lang, 1),
        aCityTxt: getLangCityName(this.data.aCityTxt, this.data.lang, 1),
        abridgeDCityTxt: this.data.abridgeDCityTxt,
        abridgeACityTxt: this.data.abridgeACityTxt,
      }
      // if(obj.abridgeACityTxt == obj.abridgeDCityTxt){
      //   return;
      // }
    }
    wx.navigateTo({
      url: `/pages/check/flightResult/flightResult?flightInfo=${JSON.stringify(obj)}`,
    })
  },
  // 获取广告
  async getPosterList() {
    this.setData({
      'getAdvertObj.language': this.data.lang,
    })
    let { result } = await getPosterList(this.data.getAdvertObj)
    if (result) {
      result.map((item) => {
        item.bannerLink = `${app.globalData.host}/attach/showPic?attachId=${item.pic}`
      })
      this.setData({
        bannerList: result,
      })
    }
  },
  // 复制网址
  copyLinkHandle(e) {
    //香港国际
    let XGUrl = {
      zh: 'https://www.hongkongairport.com/sc/flights/departures/passenger.page',
      tw: 'https://www.hongkongairport.com/tc/flights/departures/passenger.page',
      en: 'https://www.hongkongairport.com/en/flights/departures/passenger.page',
    }
    //澳门国际
    let AMUrl = {
      zh: 'https://www.macau-airport.com/cn/flight-information/flight-time-table/departure',
      tw: 'https://www.macau-airport.com/mo/flight-information/flight-time-table/departure',
      en: 'https://www.macau-airport.com/en/flight-information/flight-time-table/departure',
    }
    //中航信
    let ZHUrl = { 1: '', 2: '', 3: '' }
    let type = e.currentTarget.dataset.type
    let language = wx.getStorageSync('language')
    let url = ''
    if (type == 1) {
      url = XGUrl[language]
    } else if (type == 2) {
      url = AMUrl[language]
    } else {
      url = ZHUrl[language]
    }
    if (url == undefined) {
      wx.showToast({
        title: this.data.langData['当前没有该网址'],
        icon: 'none',
      })
    }
    wx.setClipboardData({
      data: url,
      success: () => {
        wx.showToast({
          title: this.data.langData['已复制网站网址'],
          icon: 'none',
        })
      },
    })
  },
  // 点击广告
  adClickHandle(e) {
    let link = e.currentTarget.dataset.link
    wx.setClipboardData({
      data: link,
      success: () => {
        wx.showToast({
          title: this.data.langData['已复制网站网址'],
          icon: 'none',
        })
      },
    })
  },
  // 路线规划
  routerJump() {
    wx.navigateTo({
      url: '/pages/interactionMap/interactionMap',
    })
  },
  //初始化数据
  initData() {
    const citys = getCity()
    let cityParse = JSON.parse(JSON.stringify(citys))
    delete cityParse['澳门']
    delete cityParse['澳門']
    delete cityParse['香港']
    delete cityParse['台湾']
    delete cityParse['臺灣']
    delete cityParse['Hong Kong']
    delete cityParse['Macao']
    delete cityParse['Taiwan']
    let data = {
      citys,
      cityParse,
      columnsCity: [
        {
          values: Object.keys(cityParse),
        },
        {
          values: cityParse[getDefCity()],
          defaultIndex: 1,
        },
      ],
    }
    this.setData(data)
  },
  onLoad() {
    this.se
    let lang = wx.getStorageSync('lang')
    this.setData({
      lang: lang,
      langData: _t(),
    })
    this.getPosterList()
    wx.setNavigationBarTitle({
      title: this.data.langData['航班查询'],
    })
    // this.getCityLabel()
    this.initData()
  },
})
